package com.bxait.toymail.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


import com.bxait.com.utils.PageUtils;
import com.bxait.com.utils.R;
import com.bxait.com.valid.SelectGroup;
import com.bxait.com.vo.product.AttrGroupRelationVo;
import com.bxait.toymail.product.entity.AttrEntity;
import com.bxait.toymail.product.service.AttrAttrgroupRelationService;
import com.bxait.toymail.product.service.AttrService;
import com.bxait.toymail.product.utils.AttrGroupWithAttrsVo;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.bxait.toymail.product.entity.AttrGroupEntity;
import com.bxait.toymail.product.service.AttrGroupService;

import javax.annotation.Resource;


/**
 * import org.apache.shiro.authz.annotation.RequiresPermissions;
 * 属性分组
 *
 * @author chen
 * @email 2277411582@qq.com
 * @date 2023-03-13 22:26:35
 */
@RestController
@RequestMapping("product/attrGroup")
public class AttrGroupController {

    @Resource
    @Qualifier("attrGroupService")
    private AttrGroupService attrGroupService;

    @Resource
    @Qualifier("attrService")
    private AttrService attrService;

    @Resource
    @Qualifier("attrAttrgroupRelationService")
    private AttrAttrgroupRelationService relationService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrGroupService.queryPage(params);
        return R.ok().put("page", page);
    }

    /**
     * @param params       查询携带的参数(分页参数等信息)
     * @param cateLogId    需要查询的三级分类ID
     * @==============History===============<br/>
     * @Description // TODO 查询当前分类下的所以属性分组信息
     * @Date 2023/3/15 21:58 
     * @Author <a href="qq:2277411582@qq.com">chen</a>
     * @return com.bxait.com.utils.R
     */ 
    @RequestMapping("/list/{cateLogId}")
    public R listCateLogId(
            @RequestParam Map<String, Object> params, 
            @PathVariable("cateLogId") Long cateLogId){
        PageUtils page = attrGroupService.queryPageByCateLogId(params,cateLogId);
        return R.ok().put("data", page);
    }

    /**
     * @param attrGroupId  属性分组的id
     * @==============History===============<br/>
     * @Description // TODO  获取当前属性分组的信息,并且封装当前属性分组所属分类的全路径
     * @Date 2023/3/15 23:11
     * @Author <a href="qq:2277411582@qq.com">chen</a>
     * @return com.bxait.com.utils.R
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@Validated({SelectGroup.class}) @PathVariable("attrGroupId") Long attrGroupId){
        AttrGroupEntity attrGroup = attrGroupService.getAttrGroupAndCateLogPath(attrGroupId);
        return R.ok().put("data", attrGroup);
    }


    /**
     * @param attrGroupId
     * @==============History===============<br/>
     * @Description // TODO  查询当前属性分组的全部属性
     * @Date 2023/3/18 2023/3/18
     * @Author < a href="2277411582@qq.com">chen</ a>
     * @return [java.lang.Long]
     */
    @RequestMapping(value = "/{attrGroupId}/attr/relation",method = RequestMethod.GET)
    public R listCateLogId(@PathVariable("attrGroupId") Long attrGroupId){
        List<AttrEntity> entities = attrService.getRelationAttr(attrGroupId);
        return R.ok().put("data", entities);
    }

    /**
     * @param attrGroupId  属性分组的id
     * @param params
     * @==============History===============<br/>
     * @Description // TODO  获取当前属性分组曦俺没有关联的属性,并且这个属性也没有被别的属性分组所关联
     * @Date 2023/3/19 21:05
     * @Author <a href="qq:2277411582@qq.com">chen</a>
     * @return com.bxait.com.utils.R
     */
    @GetMapping("/{attrGroupId}/attr/noAttr/relation")
    public R attrGroupRelation(@PathVariable("attrGroupId") Long attrGroupId,
                               @RequestParam Map<String, Object> params){
        PageUtils page = attrService.getNoRelationAttr(params,attrGroupId);
        return R.ok().put("data",page);
    }

    /**
     * @param cateLogId
     * @==============History===============<br/>
     * @Description // TODO  获取当前分类下的所有分组,及其分组下的所有属性
     * @Date 2023/3/20 20:57
     * @Author <a href="qq:2277411582@qq.com">chen</a>
     * @return com.bxait.com.utils.R
     */
    @GetMapping("/{cateLogId}/withAttr")
    public R getAttrGroupWithAttrs(@PathVariable("cateLogId")Long cateLogId){
        //1、查出当前分类下的所有属性分组
        //2、查出每个属性分组的所有属性
        List<AttrGroupWithAttrsVo> vos =  attrGroupService.getAttrGroupWithAttrsByCateLogId(cateLogId);
        return R.ok().put("data",vos);
    }


    /**
     * @param vos
     * @==============History===============<br/>
     * @Description // TODO  添加属性与分组关联关系
     * @Date 2023/3/18 2023/3/18
     * @Author < a href="2277411582@qq.com">chen</ a>
     * @return [com.bxait.com.vo.product.AttrGroupRelationVo[]]
     */
    @RequestMapping("/attr/relation")
    public R attrRelation(@RequestBody AttrGroupRelationVo[] vos){
        relationService.addRelation(vos);
        return R.ok();
    }

    /**
     * @param vos
     * @==============History===============<br/>
     * @Description // TODO  删除属性属性分组关联关系
     * @Date 2023/3/18 2023/3/18
     * @Author < a href="2277411582@qq.com">chen</ a>
     * @return [com.bxait.com.vo.product.AttrGroupRelationVo[]]
     */
    @RequestMapping(value = "/attr/relation/delete",method = RequestMethod.POST)
    public R deleteRelation(@RequestBody AttrGroupRelationVo[] vos){
        relationService.deleteRelation(vos);
        return R.ok();
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
